package com.aikon.wht.service;

import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * @author haitao.wang
 */
@Service
public class AutoVoteService {

    private static Logger logger = LoggerFactory.getLogger(AutoVoteService.class);

    public int vote(Integer area, Integer id) {
        String url = "http://wx.yzbtkids.com/micro/events/"+area+"/episodes/"+id+"/vote";
        String referer = "http://wx.yzbtkids.com/micro/events/"+area+"/episodes/"+id+"?from=singlemessage";
        HttpGet httpGet = new HttpGet(url);
        httpGet.setHeader("Host","wx.yzbtkids.com");
        httpGet.setHeader("Connection","keep-alive");
        httpGet.setHeader("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/wxpc,*/*;q=0.8");
        httpGet.setHeader("User-Agent","Mozilla/5.0 (Linux; Android 5.1.1; XT1570 Build/LPH23.145-22) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/37.0.0.0 Mobile MQQBrowser/6.8 TBS/036887 Safari/537.36 MicroMessenger/6.3.31.940 NetType/WIFI Language/zh_CN");
        httpGet.setHeader("Referer",referer);
        httpGet.setHeader("Accept-Encoding","gzip, deflate");
        httpGet.setHeader("Accept-Language","zh-CN,en-US;q=0.8");
        httpGet.setHeader("Cookie","user_credentials=bf39b851509e1ca8a2903cc35feae086aac0349ad7df5020cb7e37735473ce28c86e3aa05de343afece9dc32526f6da438cedebc94205d9b69beb0f2483e8f63%3A%3A25129%3A%3A2017-03-21T09%3A00%3A25%2B08%3A00; _session_id=93d3de23ea26b0fa3d875f2586c32e1a");
        HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
        HttpClient httpClient = httpClientBuilder.build();
        CommonResponseHandler crh = new CommonResponseHandler();
        int status = -1;
        try {
            status = httpClient.execute(httpGet).getStatusLine().getStatusCode();
        } catch (IOException e) {
            logger.info("Task Error.{}",e.getMessage());
        }
        return status;
    }
}
